package com.edu.util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 字符集过滤器
@WebFilter("/*")
public class CharEncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;

        req.setCharacterEncoding("utf-8"); //保证前台提交汉字不乱码
        resp.setCharacterEncoding("utf-8");
//		resp.setContentType("text/html;charset=utf-8");//向浏览器输出汉字不乱码
        resp.setContentType("application/json;charset=utf-8");

        chain.doFilter(req, resp);
    }

}
